package son.niit.ict;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

public class MainMenu extends Activity {
	
	public static final int REGISTER = 0;
	public static final int SEARCH = 1;
	public static final int MORE = 2;
	public static final int ABOUT = 3;
	public static final int EXIT = 4;

	public static Context context;

	private GridView gridView;
	private ArrayList<ScreenHomeItem> items;
	private ScreenHomeAdapter adapter;

	// ham nay lay context cua activity, neu cac ban ko su dung thi co the dung this.getApplicationContext(),this.getBaseContext()...
	@Override
	public void onStart() {
		super.onStart();
		context = this;
	}
	
	// activity nay dung de tao ra gridview, no giong nhu tao listView ma chung ta da hoc
	// gom 3 buoc:
	//b1 . tao mang chua cac item muon the hien trong gridview. buoc nay chung ta xay dung kieu phan tu mang, chinh la kieu lop gom 2 thuoc tinh bitMap va String.
	// b2. Tao ra 1 adapter de chuyen toan bo cac phan tu mang tren vao gridView.
	// b3. gan adapter do cho gridView.
	
	

	@Override
	public void onCreate(Bundle icicle) {
		super.onCreate(icicle);

		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.main_menu);

		// day la mang chua cac item muon dat vao trong gridview dau tien mang chua co phan tu nao
		this.items = new ArrayList<ScreenHomeItem>();

		// them cac phan tu vao cho mang
		this.items.add(new ScreenHomeItem(BitmapFactory.decodeResource(
				getResources(), R.drawable.cash_register), "Register"));

		// them cac phan tu vao cho mang
		this.items.add(new ScreenHomeItem(BitmapFactory.decodeResource(
				getResources(), R.drawable.gtk_find), "Search"));
		
		// them cac phan tu vao cho mang
		this.items.add(new ScreenHomeItem(BitmapFactory.decodeResource(
				getResources(), R.drawable.more), "More"));

		// them cac phan tu vao cho mang
		this.items.add(new ScreenHomeItem(BitmapFactory.decodeResource(
				getResources(), R.drawable.about_48), "About"));

		// them cac phan tu vao cho mang
		this.items.add(new ScreenHomeItem(BitmapFactory.decodeResource(
				getResources(), R.drawable.exit), "Exit"));

		// tao 1 adapter do chuyen cac phan tu cua mang vao gridview
		this.adapter = new ScreenHomeAdapter(this.items);
		
		
		this.gridView = (GridView) this.findViewById(R.id.screen_home_gridview);
		
		// gan adapter cho gridview
		this.gridView.setAdapter(this.adapter);

		// ham nay de lang gnhe khi co su kien click vao 1 item nao do cua gridview
		this.gridView_setOnItemClickListener();
	}

	@Override
	public void onResume() {
		super.onResume();
	}

	// neu dang trong giao dien nay ma nhan nut back cua dien thoai thi se ket thuc activity va thoat ra khoi chuong trinh
	// cho vao cho dep thoi
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		switch (keyCode) {

		case KeyEvent.KEYCODE_BACK:
			finish();
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}

	
	// ham nay thuc hien khi co su kien click vao cac item
	public void gridView_setOnItemClickListener() {
		this.gridView.setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
				switch (position) {
				case REGISTER:
					startActivity(new Intent(context, Register.class));
					break;
				case SEARCH:

					break;
				case MORE:
					startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("http://niithanoi.vn/")));
					break;
				case ABOUT:
					startActivity(new Intent(context, About.class));
					break;
				case EXIT:
					finish();
					break;
				
				default:
					break;
				}

			}
		});
	}

	/* ===================== Day chinh la buoc 1 ======================== */

	private class ScreenHomeItem {
		private final Bitmap icon;
		private final String text;

		private ScreenHomeItem(Bitmap icon, String text) {
			this.icon = icon;
			this.text = text;
		}
	}
	
	/* ===================== Day chinh la buoc 2 ======================== */
	// cac phuong thuc getCount(), getItem(), getItemId(), getView() thi no tu sinh khi chung ta extend tu lop BaseAdapter.

	private class ScreenHomeAdapter extends BaseAdapter {
		private ArrayList<ScreenHomeItem> items;

		private ScreenHomeAdapter(ArrayList<ScreenHomeItem> items) {
			this.items = items;
		}
     // tra ve so luong phan tu trong gridview
		public int getCount() {
			return this.items.size();
		}
     // tra ve doi tuong Item cua gridview
		public Object getItem(int position) {
			return null;
		}
     // lay id cua item ma chung da da lay
		public long getItemId(int position) {
			return 0;
		}
		// lay view cua item, o day no la viewGroup gom 2 view la imageview va Textview 
		public View getView(int position, View convertView, ViewGroup parent) {
			View view = convertView;  // lay view cua 1 item trong gridview
			ScreenHomeItem item;      // lay doi tuong item

			if (view == null) {
				// neu view null tuc la ko co gi thi gan giao dien ma chung ta thiet ket home_item cho view do
				view = getLayoutInflater().inflate(R.layout.home_item, null);
			}
			if ((item = this.items.get(position)) == null) {
				// neu doi tuong item ma chung ta lay = null thi tra ve view vua set.
				return view;
			}

			// khai bao cac thanh phan trong view vua thiet lap cho item cua gridview
			ImageView iv = (ImageView) view.findViewById(R.id.screen_home_item_icon);
			iv.setScaleType(ImageView.ScaleType.CENTER_CROP);
			TextView tv = (TextView) view.findViewById(R.id.screen_home_item_text);

			// gan gia tri cho 2 thanh phan
			tv.setText(item.text);
			iv.setImageBitmap(item.icon);

			return view;
		}
	}

}
